package com.mg.admin.controller.admin;


import com.mg.admin.bean.admin.ResourceVO;
import com.mg.admin.bean.admin.Result;
import com.mg.admin.bean.admin.UserVO;
import com.mg.admin.controller.BaseController;
import com.mg.admin.service.admin.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/admin/resource/")
public class ReourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;



    @RequestMapping(value= LIST,method = RequestMethod.GET)
    public void list(ModelMap modelMap){
        UserVO userVO = currentUser();
        String resourceId = userVO.getResourceIds();
       List<ResourceVO> result = resourceService.findById(resourceId);
        modelMap.put("list",result);
    }

    @RequestMapping(value= ADD,method = RequestMethod.GET)
    public void add(ModelMap modelMap, Long id){
        modelMap.put("parentId",id);
    }

    @RequestMapping(value= EDIT,method = RequestMethod.GET)
    public void edit(ModelMap modelMap, Long id){
        ResourceVO resourceVO = resourceService.finOne(id);
        modelMap.put("bean",resourceVO);
    }

    @RequestMapping(value= VIEW,method = RequestMethod.GET)
    public void view(ModelMap modelMap, Long id){
        ResourceVO resourceVO = resourceService.finOne(id);
        modelMap.put("bean",resourceVO);
    }

    @RequestMapping(value= DELETE,method = RequestMethod.GET)
    @ResponseBody
    public void delete(Long id){
        resourceService.delete(id);
    }

    @RequestMapping(value= SAVE,method = RequestMethod.POST)
    @ResponseBody
    public void save(ResourceVO resourceVO){
        if (resourceVO.getId() == null){
            resourceService.insertOne(resourceVO);
        }else {
            resourceService.updateOne(resourceVO);
        }
    }

    /**
     * 获得列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "treeDetailList",method = RequestMethod.POST)
    public Result<List<ResourceVO>> treeDetailList(ResourceVO resourceVO){
        List<ResourceVO> resourceVOS = resourceService.findByParentId(resourceVO.getId());
        return new Result<>(resourceVOS);
    }




}